package ace.tool.generate.code.impl.resolver.ui.resolver;

import ace.cmp.util.CaseFormatUtils;
import ace.tool.generate.code.impl.manager.common.EntityModelManager;
import ace.tool.generate.code.impl.resolver.ui.model.bo.UiApiConfigClassInfo;
import ace.tool.generate.code.impl.resolver.ui.model.bo.UiApiConfigModelInfo;
import ace.tool.generate.code.impl.resolver.ui.model.bo.UiProjectCommonInfo;
import ace.tool.generate.code.impl.resolver.ui.model.dto.UiClassFileInfo;
import ace.tool.generate.code.impl.service.model.input.GenerateInput;
import ace.tool.generate.code.impl.service.model.input.GenerateModelInput;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.Builder;
import lombok.RequiredArgsConstructor;
import org.apache.commons.text.StringSubstitutor;
import org.apache.commons.text.WordUtils;

/**
 * @author caspar
 * @date 2023/11/17 10:34
 */
@Builder
@RequiredArgsConstructor
public class UiApiConfigClassInfoResolver {
  private final GenerateInput input;
  private final UiProjectCommonInfo uiProjectCommonInfo;
  private final EntityModelManager entityModelManager = EntityModelManager.getInstance();

  public UiApiConfigClassInfo resolve() {

    UiClassFileInfo classFileInfo =
        UiClassFileInfoResolver.builder()
            .className(uiProjectCommonInfo.getProjectModuleCodeName())
            .classNameSuffix(input.getUiApiConfigClassNameSuffix())
            .classPackagePrefix(input.getUiApiConfigClassPackagePrefix())
            .rootPackageFullName("/core/" + uiProjectCommonInfo.getProjectModuleCodeName())
            .sourceCodeRootDir(uiProjectCommonInfo.getSrcDir())
            .fileNameSuffix(".ts")
            .build()
            .resolve();

    UiApiConfigClassInfo uiApiConfigClassInfo =
        UiApiConfigClassInfo.builder()
            .info(classFileInfo)
            .projectModuleName(uiProjectCommonInfo.getProjectModuleCodeName())
            .fields(this.getFields())
            .build();

    return uiApiConfigClassInfo;
  }

  private List<UiApiConfigModelInfo> getFields() {

    return this.input.getModels().stream().map(this::getField).collect(Collectors.toList());
  }

  private UiApiConfigModelInfo getField(GenerateModelInput modelInput) {
    String apiUri = this.getApiUri(modelInput);
    return UiApiConfigModelInfo.builder()
        .apiUri(apiUri)
        .name(WordUtils.uncapitalize(modelInput.getName()))
        .build();
  }

  private String getApiUri(GenerateModelInput modelInput) {
    Map<String, String> data = new HashMap<>();
    data.put("projectName", this.input.getProjectName());
    data.put(
        "entityClassNameLowercaseDashName", this.getEntityClassNameLowercaseDashName(modelInput));
    String fileName =
        StringSubstitutor.replace(
            "/${projectName}/admin/${entityClassNameLowercaseDashName}", data);
    return fileName;
  }

  private String getEntityClassNameLowercaseDashName(GenerateModelInput modelInput) {
    return CaseFormatUtils.convertUpperCamelToLowerUnderscore(modelInput.getName()).replaceAll("_", "-");
  }
}
